﻿Imports System.Data
Imports System.Data.SqlClient
Imports DevComponents.DotNetBar
'Imports Microsoft.Office.Interop.Excel

Public Class frmMain
    Private flag As Boolean = True
    '------------------------------------------- chạy frmFlash giới thiệu chương trình -----------------
    'Public Sub New()
    '    ' This call is required by the designer.
    '    InitializeComponent()

    '    ' Add any initialization after the InitializeComponent() call.
    '    Dim flash As frmFlash = New frmFlash()
    '    flash.Show()
    '    flash.Opacity = 100%
    '    System.Threading.Thread.Sleep(3000)
    '    flash.Close()
    'End Sub

    '-------------------------------------------- CHƯƠNG TRÌNH CHÍNH -------------------------------------
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '---------------------------------------- Kiểm tra đăng nhập -------------------------------------
        
        dangnhap()

        '---------------------------------------- chạy giao dien -----------------------------------------
        Timer1.Interval = 1500
        Splitter1.Height = 1200
        Timer2.Interval = 30
        Timer2.Start()
    End Sub

    '-----------------------------------------Load dữ liệu -------------------------------------------
    Private Sub DocDuLieu()
        LoadDataNV()
        contrlNV.mygrid = gvNhanVien
        LockButtonNV(True)
        LockTextboxNV(False)
        '-----------------------------------------------
        LoadDataNDT()
        ntDT.mygrid = gvNguoiDangTin
        LockTextboxNDT(False)
        txtDTMa.Enabled = False
        LockButtonNDT(True)
        '-----------------------------------------------
        LoadDataTN()
        contrlTN.mygrid = gvNguoiThueNha
        LockTextboxTN(False)
        txtTNMa.Enabled = False
        LockButtonTN(True)
        '------------------------------------------------
        LoadDataNNT()
        contrlNT.mygrid = gvNguoiNhanTin
        LockTextboxNNT(False)
        txtNNTMa.Enabled = False
        LockButtonNNT(True)
        '-------------------------------------------------
        LoadDataKV()
        LockTextboxKV(False)
        cmbMaKV.Enabled = False
        LockButtonKV(True)
        '----------------------------------------------------
        LoadDataLN()
        LockTextboxLN(False)
        cmbLoaiNha.Enabled = False
        LockButtonLN(True)
        '---------------------------------------------------
        LoadDataTT()
        LockTextboxTT(False)
        contrlTT.mygrid = gvThongTinNha
        txtTTMa.Enabled = False
        LockButtonTT(True)

    End Sub

    Private Sub dangnhap()
        ClearBindingNV()
        ClearTextboxNV()
        ClearBinding()
        ClearTextbox()
        ClearBindingNDT()
        ClearTextboxNDT()
        ClearBindingNNT()
        ClearBindingNNT()
        ClearBindingTN()
        ClearTextboxTN()
        ClearBindingLN()
        ClearTextboxLN()
        ClearBindingTT()
        ClearTextboxTT()
        frmDangNhap.ShowDialog()
        If frmDangNhap.LoginLoaiND = "" Then
            Me.Close()
        Else
            lbThongBao.Text = frmDangNhap.XinChao.ToString
            If frmDangNhap.LoginLoaiND = "Quan_Ly" OrElse frmDangNhap.LoginLoaiND = "Admin" Then
                DocDuLieu()
                btnDangNhap.Enabled = False
            Else
                frmPhanQuyen.UpdateQuyen()
                DocDuLieu()
                btnNVThem.Enabled = False
                btnNVSua.Enabled = False
                btnNVXoa.Enabled = False
                btnLoaiNV.Enabled = False
                gvNhanVien.Visible = False
                btnSaoLuu.Enabled = False
                btnPhucHoi.Enabled = False
                btnDangNhap.Enabled = False
                bdNV.Enabled = False
                GroupPanel2.Enabled = False
                Me.btnPhanQuyen.Text = "Xem Quyền"
            End If
        End If
    End Sub

    '----------------------------------------------- Hàm xử lý giao dien ----------------------------------
#Region "Xử lý chạy chữ"
    Private Sub Time_ChayChu_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer_ChayChu.Tick
        If lblChayChu.Left < -250 Then
            lblChayChu.Left = 1550
            flag = Not flag
        Else
            lblChayChu.Left -= 10
            If flag Then
                lblChayChu.ForeColor = Color.Black
                flag = Not flag
            Else
                lblChayChu.ForeColor = Color.Teal
                flag = Not flag
            End If
        End If
    End Sub
    Private Sub RibbonPanel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If flag Then
            Timer_ChayChu.Stop()
            flag = Not flag
        Else
            Timer_ChayChu.Start()
            flag = Not flag
        End If
    End Sub

#End Region

#Region "Xử lý đồng hồ thời gian"
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Time.Text = "<div align=""center"">" + DateTime.Now.ToShortDateString + "<br/>" + DateTime.Now.ToShortTimeString + "</div>"
    End Sub
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        If Splitter1.Height = 3 Then
            Timer2.Stop()
        Else
            Splitter1.Height = Splitter1.Height - 20
        End If
    End Sub
#End Region

    '---------------------------------------------- Xử lý nút logout và thoat chương trinh ----------------
#Region "Xử lý thoát chương trình, logout"
    Private Sub btnThoat1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat1.Click
        Me.Close()
    End Sub
    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        On Error Resume Next
        Me.WindowState = 0
        Do
            Me.Top = Me.Top + 10
            Me.Left = Me.Left + 10
            Me.Width = Me.Width - 30
            Me.Height = Me.Height - 30
        Loop Until Me.Top >= Me.Height
        End
        frmDangNhap.Close()
    End Sub

    Private Sub btnLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogout.Click
        lbThongBao.Text = "Đăng nhập thệ thống để sử dụng chương trình!"
        Login()
    End Sub

    Private Sub FormMain_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        frmDangNhap.Timer1.Start()
        frmDangNhap.txtUser.Text = ""
        frmDangNhap.txtPassWord.Text = ""
        frmDangNhap.Opacity = True
        frmDangNhap.Opacity = 100%
    End Sub
#End Region

    '--------------------------------------------- xử lý nút file -------------------------------------
#Region "Xử lý các chức năng trong nút file"
    Private Sub btnSaoLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaoLuu.Click
        frmBackupAndRestore.rdSaoLuu.Checked = True
        frmBackupAndRestore.ShowDialog()
    End Sub

    Private Sub btnPhucHoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPhucHoi.Click
        frmBackupAndRestore.rdPhucHoi.Checked = True
        frmBackupAndRestore.ShowDialog()
    End Sub

    Private Sub btnDoiMatKhau_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDoiMatKhau.Click
        frmDoiMatKhau.ShowDialog()
    End Sub

    Private Sub btnDangXuat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDangXuat.Click
        Defaul()
        lbThongBao.Text = "Đăng nhập thệ thống để sử dụng chương trình!"
    End Sub

    Private Sub btnDangNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDangNhap.Click
        Login()
    End Sub

    Public Sub Defaul()
        TabItem_NV.Visible = False
        TabItem_DangTin.Visible = False
        TabItem_ThueNha.Visible = False
        TabItem_NhanTin.Visible = False
        TabItem_LN_KV.Visible = False
        TabItem_TTN.Visible = False
        btnQuanLyNV.Enabled = False
        btnDangTin.Enabled = False
        btnThueNha.Enabled = False
        btnNhanTin.Enabled = False
        btnLN_KV.Enabled = False
        btnQL_TTN.Enabled = False
        btnLogout.Enabled = False
        btnDangXuat.Enabled = False
        btnDoiMatKhau.Enabled = False
        btnPhucHoi.Enabled = False
        btnSaoLuu.Enabled = False
        btnDangNhap.Enabled = True
    End Sub

    Private Sub Login()

        frmDangNhap.txtUser.Text = ""
        frmDangNhap.txtPassWord.Text = ""
        frmDangNhap.Timer1.Start()
        
        Dim dangnhap As frmDangNhap = New frmDangNhap
        dangnhap.ShowDialog()
        If dangnhap.LoginLoaiND = "" Then
            Me.Close()
        Else

            If dangnhap.LoginLoaiND = "Quan_Ly" OrElse dangnhap.LoginLoaiND = "Admin" Then
                lbThongBao.Text = dangnhap.XinChao.ToString
                ClearBindingNV()
                ClearTextboxNV()
                ClearBinding()
                ClearTextbox()
                ClearBindingNDT()
                ClearTextboxNDT()
                ClearBindingNNT()
                ClearBindingNNT()
                ClearBindingTN()
                ClearTextboxTN()
                ClearBindingLN()
                ClearTextboxLN()
                ClearBindingTT()
                ClearTextboxTT()
                TabItem_NV.Visible = True
                TabItem_DangTin.Visible = True
                TabItem_ThueNha.Visible = True
                TabItem_NhanTin.Visible = True
                TabItem_LN_KV.Visible = True
                TabItem_TTN.Visible = True
                btnQuanLyNV.Enabled = True
                btnDangTin.Enabled = True
                btnThueNha.Enabled = True
                btnNhanTin.Enabled = True
                btnLN_KV.Enabled = True
                btnQL_TTN.Enabled = True
                btnLogout.Enabled = True
                btnDangXuat.Enabled = True
                btnDoiMatKhau.Enabled = True
                btnPhucHoi.Enabled = True
                btnSaoLuu.Enabled = True
                gvNhanVien.Visible = True
                GroupPanel2.Enabled = True
                GroupPanel1.Enabled = True
                bdNV.Enabled = True
                DocDuLieu()
                btnDangNhap.Enabled = False
            Else
                frmDangNhap.LoginLoaiND = dangnhap.LoginLoaiND
                frmDangNhap.LoginPassND = dangnhap.LoginPassND
                frmDangNhap.LoginTenND = dangnhap.LoginTenND

                lbThongBao.Text = dangnhap.XinChao.ToString
                ClearBindingNV()
                ClearTextboxNV()
                ClearBinding()
                ClearTextbox()
                ClearBindingNDT()
                ClearTextboxNDT()
                ClearBindingNNT()
                ClearBindingNNT()
                ClearBindingTN()
                ClearTextboxTN()
                ClearBindingLN()
                ClearTextboxLN()
                ClearBindingTT()
                ClearTextboxTT()
                TabItem_NV.Visible = True
                TabItem_DangTin.Visible = True
                TabItem_ThueNha.Visible = True
                TabItem_NhanTin.Visible = True
                TabItem_LN_KV.Visible = True
                TabItem_TTN.Visible = True
                btnQuanLyNV.Enabled = True
                btnDangTin.Enabled = True
                btnThueNha.Enabled = True
                btnNhanTin.Enabled = True
                btnLN_KV.Enabled = True
                btnQL_TTN.Enabled = True
                btnLogout.Enabled = True
                btnDangXuat.Enabled = True
                btnDoiMatKhau.Enabled = True
                btnPhucHoi.Enabled = True
                btnSaoLuu.Enabled = True
                frmPhanQuyen.UpdateQuyen()
                DocDuLieu()
                btnDangNhap.Enabled = False
                btnNVThem.Enabled = False
                btnNVSua.Enabled = False
                btnNVXoa.Enabled = False
                btnLoaiNV.Enabled = False
                gvNhanVien.Visible = False
                btnSaoLuu.Enabled = False
                btnPhucHoi.Enabled = False
                btnDangNhap.Enabled = False
                bdNV.Enabled = False
                GroupPanel2.Enabled = False
                Me.btnPhanQuyen.Text = "Xem Quyền"
            End If
        End If
    End Sub
#End Region

    '--------------------------------------------- xử lý nút trợ giúp -------------------------------------
#Region "Xử lý nút trợ giúp"
    Private Sub ButtonItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem8.Click
        frmHelp.ShowDialog()
    End Sub
#End Region

    '-------------------------------------------- xử lý các nút điều kiển bên trái ------------------------
#Region "Xử lý các nút bên trái để chuyển tab"
    Private Sub btnQuanLyNV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuanLyNV.Click
        Me.TabControl_Main.SelectedTabIndex = 0
    End Sub

    Private Sub btnDangTin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDangTin.Click
        Me.TabControl_Main.SelectedTabIndex = 1
    End Sub

    Private Sub btnThueNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThueNha.Click
        Me.TabControl_Main.SelectedTabIndex = 2
    End Sub

    Private Sub btnNhanTin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhanTin.Click
        Me.TabControl_Main.SelectedTabIndex = 3
    End Sub

    Private Sub btnLN_KV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLN_KV.Click
        Me.TabControl_Main.SelectedTabIndex = 5
    End Sub

    Private Sub ButtonX18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQL_TTN.Click
        Me.TabControl_Main.SelectedTabIndex = 4
    End Sub

#End Region

    '-------------------------------------------- xử lý các hàm load dữ liệu ------------------------------
#Region "xử lý Load dữ liệu khu vực"
    Private kvBUS As KhuVucBUS
    Private _emp As KhuVucDTO
    Private cm As CurrencyManager
    Private flagIsUp As Boolean

    Private Sub LoadDataKV()
        gvKhuVuc.ClearSelection()
        kvBUS = New KhuVucBUS()
        Dim empList As New List(Of KhuVucDTO)
        empList = kvBUS.Getdata()
        cm = Me.BindingContext(empList)

        cmbMaKV.DataBindings.Add("Text", empList, "Mã_khu_vực")
        txtTenKV.DataBindings.Add("Text", empList, "Tên_khu_vực")

        gvKhuVuc.DataSource = empList
    End Sub

    Private Sub ClearBinding()
        cmbMaKV.DataBindings.Clear()
        txtTenKV.DataBindings.Clear()
    End Sub

    Private Sub LockTextboxKV(ByVal f As Boolean)
        txtTenKV.Enabled = f
        If f = False Then
            txtTenKV.BackColor = Color.LemonChiffon
        Else
            txtTenKV.BackColor = Color.White
        End If
    End Sub

    Private Sub ClearTextbox()
        cmbMaKV.Text = ""
        txtTenKV.Text = ""
    End Sub

    Private Sub LockButtonKV(ByVal dt As Boolean)
        btnKVThem.Enabled = dt
        btnKVSua.Enabled = dt
        btnKVXoa.Enabled = dt
        btnKVLuu.Enabled = Not dt
        btnKVHuy.Enabled = Not dt
    End Sub

    Private Sub btnKVSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKVSua.Click
        flagIsUp = False
        LockTextboxKV(True)
        LockButtonKV(False)
        gvKhuVuc.Enabled = False
        txtTenKV.Focus()
    End Sub

    Private Sub btnKVHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKVHuy.Click
        ClearBinding()
        LoadDataKV()
        LockTextboxKV(False)
        LockButtonKV(True)
        gvKhuVuc.Enabled = True
    End Sub

    Private Sub btnKVThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKVThem.Click
        flagIsUp = True
        LockTextboxKV(True)
        LockButtonKV(False)
        ClearTextbox()
        gvKhuVuc.Enabled = False
        txtTenKV.Focus()
    End Sub

    Private Sub btnKVLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKVLuu.Click
        Dim MaKV As String = cmbMaKV.Text
        Dim TenKV As String = txtTenKV.Text
        '-------------------------------------'
        If TestInfo() Then
            _emp = New KhuVucDTO(MaKV, TenKV)
            kvBUS = New KhuVucBUS()
            kvBUS.IsUpKV(_emp, flagIsUp)
            ClearBinding()
            LoadDataKV()
            LockTextboxKV(False)
            LockButtonKV(True)
            gvKhuVuc.Enabled = True
            MsgBox("Đã cập nhật dữ liệu thành công", MsgBoxStyle.Information, "Thông báo")
        End If
    End Sub

    Private Function TestInfo() As Boolean
        TestInfo = True
        If Trim(txtTenKV.Text) = "" Then
            TestInfo = False
            MsgBox("Têm khu vực không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTenKV.Focus()
            Exit Function
        End If
        Exit Function

    End Function

    Private Sub btnKVXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKVXoa.Click
        Dim MaKV As String = cmbMaKV.Text
        Dim qs = MsgBox("Bạn chắc chắn muốn xóa tất cả thông tin về:" _
                        & vbCrLf & vbCrLf & " - Ma khu vực: " & cmbMaKV.Text _
                        & vbCrLf & vbCrLf & " - Tên: " & txtTenKV.Text & " " & "", _
                        vbInformation + vbYesNo, "Thông báo")
        If qs = vbYes Then
            _emp = New KhuVucDTO(MaKV)
            kvBUS = New KhuVucBUS()
            kvBUS.DeleteKV(_emp)
            ClearBinding()
            LoadDataKV()
            LockTextboxKV(False)
            LockButtonKV(True)
            gvKhuVuc.Enabled = True
            MsgBox("Dữ liệu đã xóa thành công", vbInformation, "Thông báo")
        Else
            MsgBox("Đã hủy thao tác xóa!", vbExclamation, "Thông báo")
        End If
    End Sub

    Private Sub ButtonX4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX4.Click
        connect = New ConnectDB()
        Dim str As String
        Dim dt As DataTable = New DataTable()
        If TextBoxX2.Text = "" Then
            MsgBox("Phải điền thông tin tìm!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            TextBoxX2.Focus()
            Exit Sub
        End If
        If RadioButton8.Checked = True Then
            str = "Select * From KHUVUC WHERE MaKhuVuc LIKE'%" & TextBoxX2.Text & "%'"
        Else
            str = "Select * From KHUVUC WHERE TenKhuVuc LIKE'%" & TextBoxX2.Text & "%'"
        End If
        Try
            ClearTextbox()
            gvKhuVuc.ClearSelection()
            dt = connect.GetData(str)
            connect.Close()
            gvKhuVuc.DataSource = dt
        Catch ex As Exception
            MsgBox("Không có!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
        End Try
    End Sub

    Private Sub ButtonX8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX8.Click
        RadioButton8.Checked = True
        TextBoxX2.Text = ""
        ClearBinding()
        ClearTextbox()
        LoadDataKV()
    End Sub

#End Region

#Region "Xử lý load dữ liệu Loại nhà"
    Private lnBUS As LoaiNhaBUS
    Private _ln As LoaiNhaDTO
    Private ln As CurrencyManager
    Private flagIsUpLN As Boolean

    Private Sub LoadDataLN()
        gvLoaiNha.ClearSelection()
        lnBUS = New LoaiNhaBUS()
        Dim lnList As New List(Of LoaiNhaDTO)
        lnList = lnBUS.Getdata()
        ln = Me.BindingContext(lnList)

        cmbLoaiNha.DataBindings.Add("Text", lnList, "Mã_loại_nhà")
        txtTenLoaiNha.DataBindings.Add("Text", lnList, "Tên_loại_nhà")

        gvLoaiNha.DataSource = lnList
    End Sub

    Private Sub ClearBindingLN()
        cmbLoaiNha.DataBindings.Clear()
        txtTenLoaiNha.DataBindings.Clear()
    End Sub

    Private Sub ClearTextboxLN()
        cmbLoaiNha.Text = ""
        txtTenLoaiNha.Text = ""
    End Sub

    Private Sub LockTextboxLN(ByVal f As Boolean)
        txtTenLoaiNha.Enabled = f
        If f = False Then
            txtTenLoaiNha.BackColor = Color.LemonChiffon
        Else
            txtTenLoaiNha.BackColor = Color.White
        End If
    End Sub

    Private Sub LockButtonLN(ByVal dt As Boolean)
        btnLNThem.Enabled = dt
        btnLNSua.Enabled = dt
        btnLNXoa.Enabled = dt
        btnLNLuu.Enabled = Not dt
        btnLNHuy.Enabled = Not dt
    End Sub

    Private Sub btnLNSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLNSua.Click
        flagIsUpLN = False
        LockTextboxLN(True)
        LockButtonLN(False)
        gvLoaiNha.Enabled = False
        txtTenLoaiNha.Focus()
    End Sub

    Private Sub btnLNThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLNThem.Click
        flagIsUpLN = True
        LockTextboxLN(True)
        LockButtonLN(False)
        ClearTextboxLN()
        gvLoaiNha.Enabled = False
        txtTenLoaiNha.Focus()
    End Sub

    Private Sub btnLNHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLNHuy.Click
        ClearBindingLN()
        LoadDataLN()
        LockTextboxLN(False)
        LockButtonLN(True)
        gvLoaiNha.Enabled = True
    End Sub

    Private Sub btnLNLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLNLuu.Click
        Dim MaKV As String = cmbMaKV.Text
        Dim TenKV As String = txtTenKV.Text
        '-------------------------------------'
        If TestInfo() Then
            _ln = New LoaiNhaDTO(MaKV, TenKV)
            lnBUS = New LoaiNhaBUS()
            lnBUS.IsUpLN(_ln, flagIsUp)
            ClearBindingLN()
            LoadDataLN()
            LockTextboxLN(False)
            LockButtonLN(True)
            gvLoaiNha.Enabled = True
            MsgBox("Đã cập nhật dữ liệu thành công", MsgBoxStyle.Information, "Thông báo")
        End If
    End Sub
    Private Function TestInfoLN() As Boolean
        TestInfoLN = True
        If Trim(txtTenLoaiNha.Text) = "" Then
            TestInfoLN = False
            MsgBox("Têm khu vực không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTenLoaiNha.Focus()
            Exit Function
        End If
        Exit Function

    End Function
    Private Sub btnLNXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLNXoa.Click
        Dim MaLN As String = cmbLoaiNha.Text
        Dim qs = MsgBox("Bạn chắc chắn muốn xóa tất cả thông tin về:" _
                        & vbCrLf & vbCrLf & " - Ma Loại Nhà: " & cmbLoaiNha.Text _
                        & vbCrLf & vbCrLf & " - Tên: " & txtTenLoaiNha.Text & " " & "", _
                        vbInformation + vbYesNo, "Thông báo")
        If qs = vbYes Then
            _ln = New LoaiNhaDTO(MaLN)
            lnBUS = New LoaiNhaBUS()
            lnBUS.DeleteLN(_ln)
            ClearBindingLN()
            LoadDataLN()
            LockTextboxLN(False)
            LockButtonLN(True)
            gvKhuVuc.Enabled = True
            MsgBox("Dữ liệu đã xóa thành công", vbInformation, "Thông báo")
        Else
            MsgBox("Đã hủy thao tác xóa!", vbExclamation, "Thông báo")
        End If
    End Sub

    Private Sub ButtonX3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX3.Click
        connect = New ConnectDB()
        Dim str As String
        Dim dt As DataTable = New DataTable()
        If TextBoxX4.Text = "" Then
            MsgBox("Phải điền thông tin tìm!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            TextBoxX4.Focus()
            Exit Sub
        End If
        If RadioButton10.Checked = True Then
            str = "Select * From LOAINHA WHERE MaLoai LIKE'%" & TextBoxX4.Text & "%'"
        Else
            str = "Select * From LOAINHA WHERE TenLoaiNha LIKE'%" & TextBoxX4.Text & "%'"
        End If
        Try
            ClearTextboxLN()
            gvLoaiNha.ClearSelection()
            dt = connect.GetData(str)
            connect.Close()
            gvLoaiNha.DataSource = dt
        Catch ex As Exception
            MsgBox("Không có!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
        End Try
    End Sub

    Private Sub ButtonX7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX7.Click
        TextBoxX4.Text = ""
        RadioButton10.Checked = True
        ClearBindingLN()
        ClearTextboxLN()
        LoadDataLN()
    End Sub

#End Region

#Region "Xử lý load dữ liệu người nhận tin"
    Private nntBUS As NguoiNhanTinBUS
    Private _nnt As NguoiNhanTinDTO
    Private nnt As CurrencyManager
    Private flagIsUpNNT As Boolean

    Private Sub LoadDataNNT()
        gvNguoiNhanTin.ClearSelection()
        nntBUS = New NguoiNhanTinBUS()
        Dim nnList As New List(Of NguoiNhanTinDTO)
        nnList = nntBUS.Getdata()
        nnt = Me.BindingContext(nnList)

        txtNNTMa.DataBindings.Add("Text", nnList, "Mã_NT")
        txtNNTHoTen.DataBindings.Add("Text", nnList, "Họ_tên")
        txtNNTEmail.DataBindings.Add("Text", nnList, "Email")
        txtNNTGia.DataBindings.Add("Text", nnList, "Giá_tiền")
        With cmbNNTMaKV
            .DataSource = nnList
            .DisplayMember = "Mã_Khu_Vực"
            .ValueMember = "Mã_Khu_Vực"
            .SelectedValue = "Mã_Khu_Vực"
        End With
        With cmbNNTMaLN
            .DataSource = nnList
            .DisplayMember = "Mã_loại_nhà"
            .ValueMember = "Mã_loại_nhà"
            .SelectedValue = "Mã_loại_nhà"
        End With

        gvNguoiNhanTin.DataSource = nnList
    End Sub

    Private Sub ClearBindingNNT()
        txtNNTMa.DataBindings.Clear()
        txtNNTHoTen.DataBindings.Clear()
        txtNNTEmail.DataBindings.Clear()
        txtNNTGia.DataBindings.Clear()
    End Sub

    Private Sub LockTextboxNNT(ByVal f As Boolean)
        txtNNTHoTen.Enabled = f
        txtNNTEmail.Enabled = f
        txtNNTGia.Enabled = f
        cmbNNTMaKV.Enabled = f
        cmbNNTMaLN.Enabled = f
        If f = False Then
            txtNNTHoTen.BackColor = Color.LemonChiffon
            txtNNTEmail.BackColor = Color.LemonChiffon
            txtNNTGia.BackColor = Color.LemonChiffon
            cmbNNTMaKV.BackColor = Color.LemonChiffon
            cmbNNTMaLN.BackColor = Color.LemonChiffon
        Else
            txtNNTHoTen.BackColor = Color.White
            txtNNTEmail.BackColor = Color.White
            txtNNTGia.BackColor = Color.White
            cmbNNTMaKV.BackColor = Color.White
            cmbNNTMaLN.BackColor = Color.White
        End If
    End Sub

    Private Sub ClearTextboxNNT()
        txtNNTMa.Text = ""
        txtNNTHoTen.Text = ""
        txtNNTEmail.Text = ""
        txtNNTGia.Text = ""
    End Sub

    Private Sub LockButtonNNT(ByVal dt As Boolean)
        btnNNTThem.Enabled = dt
        btnNNTSua.Enabled = dt
        btnNNTXoa.Enabled = dt
        btnNNTLuu.Enabled = Not dt
        btnNNTHuy.Enabled = Not dt
    End Sub

    Private Sub btnNNTSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNNTSua.Click
        SuaNNT()
    End Sub
    Private Sub SuaNNT()
        flagIsUpNNT = False
        LockTextboxNNT(True)
        LockButtonNNT(False)
        connect = New ConnectDB()
        Dim i As Integer
        Dim kv, ln As New DataTable
        kv = connect.GetData("Select MaKhuVuc From KHUVUC")
        For i = 0 To kv.Rows.Count() - 1
            kv.Rows(i)("MaKhuVuc").ToString()
        Next
        ln = connect.GetData("Select MaLoai From LOAINHA")
        For i = 0 To ln.Rows.Count() - 1
            ln.Rows(i)("MaLoai").ToString()
        Next
        connect.Close()
        cmbNNTMaKV.DataSource = kv
        cmbNNTMaKV.DisplayMember = "MaKhuVuc"
        cmbNNTMaKV.SelectedIndex = 0

        cmbNNTMaLN.DataSource = ln
        cmbNNTMaLN.DisplayMember = "MaLoai"
        cmbNNTMaLN.SelectedIndex = 0

        gvNguoiNhanTin.Enabled = False
        txtNNTHoTen.Focus()
    End Sub

    Private Sub btnNNTHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNNTHuy.Click
        ClearBindingNNT()
        LoadDataNNT()
        LockTextboxNNT(False)
        LockButtonNNT(True)
        gvNguoiNhanTin.Enabled = True
    End Sub

    Private Sub btnNNTThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNNTThem.Click
        ThemNNT()
    End Sub
    Private Sub ThemNNT()
        flagIsUpNNT = True
        LockTextboxNNT(True)
        LockButtonNNT(False)
        ClearTextboxNNT()
        connect = New ConnectDB()
        Dim i As Integer
        Dim kv, ln As New DataTable
        kv = connect.GetData("Select MaKhuVuc From KHUVUC")
        For i = 0 To kv.Rows.Count() - 1
            kv.Rows(i)("MaKhuVuc").ToString()
        Next
        ln = connect.GetData("Select MaLoai From LOAINHA")
        For i = 0 To ln.Rows.Count() - 1
            ln.Rows(i)("MaLoai").ToString()
        Next
        connect.Close()
        cmbNNTMaKV.DataSource = kv
        cmbNNTMaKV.DisplayMember = "MaKhuVuc"
        cmbNNTMaKV.SelectedIndex = 0

        cmbNNTMaLN.DataSource = ln
        cmbNNTMaLN.DisplayMember = "MaLoai"
        cmbNNTMaLN.SelectedIndex = 0

        gvNguoiNhanTin.Enabled = False
        txtNNTHoTen.Focus()
    End Sub

    Private Sub btnNNTLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNNTLuu.Click
        
    End Sub
    Private Sub LuuNNT()
        Dim Ma As String = txtTNMa.Text
        Dim Hoten As String = txtTNHoTen.Text
        Dim Email As String = txtTNEmail.Text
        Dim Gia As String = txtNNTGia.Text
        Dim Khuvuc As String = cmbTNMaKV.Text
        Dim Loainha As String = cmbTNMaLN.Text
        '-------------------------------------'
        If TestInfoNNT() Then
            _nnt = New NguoiNhanTinDTO(Ma, Hoten, Email, Gia, Khuvuc, Loainha)
            nntBUS = New NguoiNhanTinBUS()
            nntBUS.IsUpNNT(_nnt, flagIsUpNNT)
            ClearBindingNNT()
            LoadDataNNT()
            LockTextboxNNT(False)
            LockButtonNNT(True)
            gvNguoiNhanTin.Enabled = True
            MsgBox("Đã cập nhật dữ liệu thành công", MsgBoxStyle.Information, "Thông báo")
        End If
    End Sub

    Private Function TestInfoNNT() As Boolean
        TestInfoNNT = True
        If Trim(txtNNTHoTen.Text) = "" Then
            TestInfoNNT = False
            MsgBox("Họ tên không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtNNTHoTen.Focus()
            Exit Function
        ElseIf Trim(txtNNTEmail.Text) = "" Then
            TestInfoNNT = False
            MsgBox("Email không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtNNTEmail.Focus()
            Exit Function
        ElseIf Trim(txtNNTGia.Text) = "" Then
            TestInfoNNT = False
            MsgBox("Giá tiền không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtNNTGia.Focus()
            Exit Function
        End If
    End Function

    Private Sub btnNNTXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNNTXoa.Click
        XoaNNT()
    End Sub
    Private Sub XoaNNT()
        Dim EmpID As String = txtNNTMa.Text
        Dim qs = MsgBox("Bạn chắc chắn muốn xóa tất cả thông tin về:" _
                        & vbCrLf & vbCrLf & " - Người nhân tin: " & txtNNTMa.Text _
                        & vbCrLf & vbCrLf & " - Tên: " & txtNNTHoTen.Text & " ", _
                        vbInformation + vbYesNo, "Thông báo")
        If qs = vbYes Then
            _nnt = New NguoiNhanTinDTO(EmpID)
            nntBUS = New NguoiNhanTinBUS()
            nntBUS.DeleteNNT(_nnt)
            ClearBindingNNT()
            LoadDataNNT()
            LockTextboxNNT(False)
            LockButtonNNT(True)
            gvNguoiNhanTin.Enabled = True
            MsgBox("Dữ liệu đã xóa thành công", vbInformation, "Thông báo")
        Else
            MsgBox("Đã hủy thao tác xóa!", vbExclamation, "Thông báo")
        End If
    End Sub
    Private Sub ButtonX14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX14.Click
        connect = New ConnectDB()
        Dim str As String
        Dim dt As DataTable = New DataTable()
        If txtNN.Text = "" Then
            MsgBox("Phải điền thông tin tìm!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtNN.Focus()
            Exit Sub
        End If
        If rdNN.Checked = True Then
            str = "Select * From NGUOINHANTIN WHERE MaNguoiNhan LIKE'%" & txtNN.Text & "%'"
        Else
            str = "Select * From NGUOINHANTIN WHERE HoTenNN LIKE'%" & txtNN.Text & "%'"
        End If
        Try
            ClearTextboxNNT()
            gvNguoiNhanTin.ClearSelection()
            dt = connect.GetData(str)
            connect.Close()
            gvNguoiNhanTin.DataSource = dt
        Catch ex As Exception
            MsgBox("Không có!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
        End Try
    End Sub
#End Region
#Region "người nhận tin 2"
    Private Sub ButtonItem7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem7.Click
        ThemNNT()
    End Sub

    Private Sub ButtonItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem9.Click
        XoaNNT()
    End Sub

    Private Sub ButtonItem10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem10.Click
        SuaNNT()
    End Sub

    Private Sub ButtonItem11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem11.Click
        LuuNNT()
    End Sub

    Private Sub ButtonItem12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem12.Click
        txtNN.Text = ""
        rdNN.Checked = True
        ClearBindingNNT()
        ClearTextboxNNT()
        LoadDataNNT()
    End Sub
    Private Sub ButtonX2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX2.Click
        txtNN.Text = ""
        rdNN.Checked = True
        ClearBindingNNT()
        ClearTextboxNNT()
        LoadDataNNT()
    End Sub
    Private Sub ButtonItem13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem13.Click

    End Sub
    Private Sub ButtonItem14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem14.Click
        'verfying the datagridview having data or not
        If ((gvNguoiNhanTin.Columns.Count = 0) Or (gvNguoiNhanTin.Rows.Count = 0)) Then
            Exit Sub
        End If

        'Creating dataset to export
        Dim dset As New DataSet
        'add table to dataset
        dset.Tables.Add()
        'add column to that table
        For i As Integer = 0 To gvNguoiNhanTin.ColumnCount - 1
            dset.Tables(0).Columns.Add(gvNguoiNhanTin.Columns(i).HeaderText)
        Next
        'add rows to the table
        Dim dr1 As DataRow
        For i As Integer = 0 To gvNguoiNhanTin.RowCount - 1
            dr1 = dset.Tables(0).NewRow
            For j As Integer = 0 To gvNguoiNhanTin.Columns.Count - 1
                dr1(j) = gvNguoiNhanTin.Rows(i).Cells(j).Value
            Next
            dset.Tables(0).Rows.Add(dr1)
        Next

        Dim excel As New Microsoft.Office.Interop.Excel.Application  '.ApplicationClass
        Dim wBook As Microsoft.Office.Interop.Excel.Workbook
        Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

        wBook = excel.Workbooks.Add()
        wSheet = wBook.ActiveSheet()

        Dim dt As System.Data.DataTable = dset.Tables(0)
        Dim dc As System.Data.DataColumn
        Dim dr As System.Data.DataRow
        Dim colIndex As Integer = 0
        Dim rowIndex As Integer = 0

        For Each dc In dt.Columns
            colIndex = colIndex + 1
            excel.Cells(1, colIndex) = dc.ColumnName
        Next

        For Each dr In dt.Rows
            rowIndex = rowIndex + 1
            colIndex = 0
            For Each dc In dt.Columns
                colIndex = colIndex + 1
                excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)

            Next
        Next

        wSheet.Columns.AutoFit()
        frmExportvaImportToExcel.ShowDialog()
        If frmExportvaImportToExcel.duongdan = "" Then
            Exit Sub
        End If
        Dim strFileName As String = frmExportvaImportToExcel.duongdan.ToString
        Dim blnFileOpen As Boolean = False
        Try
            Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
            fileTemp.Close()
        Catch ex As Exception
            blnFileOpen = False
        End Try

        If System.IO.File.Exists(strFileName) Then
            System.IO.File.Delete(strFileName)
        End If

        wBook.SaveAs(strFileName)
        excel.Workbooks.Open(strFileName)
        excel.Visible = True
    End Sub
#End Region

#Region "xử lý load dữ liệu thông tin nhân viên"
    Private nvBUS As NhanVienBUS
    Private _nv As NhanVienDTO
    Private nv As CurrencyManager
    Private flagIsUpNV As Boolean
    Private connect As ConnectDB

    Private Sub btnPhanQuyen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPhanQuyen.Click
        If frmDangNhap.LoginLoaiND.ToString = "Nhan_Vien" Then
            Dim frm_PhanQuyen As New frmPhanQuyen
            frm_PhanQuyen.Show()
        ElseIf cmbLoaiNV.Text = "Nhan_Vien" Then
            Dim frm_PhanQuyen As New frmPhanQuyen
            frm_PhanQuyen.Show()
        Else
            MessageBox.Show("Bạn chỉ được cấp quyền cho nhân viên thôi, vui lòng chọn 1 nhân viên!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Hand)
        End If
    End Sub
    Private Sub ButtonX13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX13.Click
        frmQuyDinh.ShowDialog()
    End Sub

    Private Sub btnLoaiNV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoaiNV.Click
        frmLoaiNhanVien.ShowDialog()
    End Sub

    Public Function LoaiNhanVien() As DataTable
        connect = New ConnectDB()
        Dim i As Integer
        Dim dt As New DataTable
        dt = connect.GetData("Select TenLoaiNV From LOAINHANVIEN")
        For i = 0 To dt.Rows.Count() - 1
            dt.Rows(i)("ten").ToString()
        Next
        connect.Close()
        Return dt
    End Function

    Private Sub LoadDataNV()
        gvNhanVien.ClearSelection()
        nvBUS = New NhanVienBUS()
        Dim empList As New List(Of NhanVienDTO)
        empList = nvBUS.Getdata()
        nv = Me.BindingContext(empList)

        cmbUserName.DataBindings.Add("Text", empList, "Tên_đăng_nhập")
        txtMatKhau.DataBindings.Add("Text", empList, "Mật_khẩu")
        txtHoTen.DataBindings.Add("Text", empList, "Họ_tên")
        dateNgaySinh.DataBindings.Add("Text", empList, "Ngày_sinh")
        txtNVGioiTinh.DataBindings.Add("Text", empList, "Giới_tính")
        txtDiaChi.DataBindings.Add("Text", empList, "Địa_chỉ")
        txtDienThoai.DataBindings.Add("Text", empList, "Điện_thoại")
        With cmbLoaiNV
            .DataSource = empList
            .DisplayMember = "Mã_loại_NV"
            .ValueMember = "Mã_loại_NV"
            .SelectedValue = "Mã_loại_NV"
        End With

        gvNhanVien.DataSource = empList
    End Sub

    Private Sub ClearBindingNV()
        cmbUserName.DataBindings.Clear()
        txtMatKhau.DataBindings.Clear()
        txtHoTen.DataBindings.Clear()
        dateNgaySinh.DataBindings.Clear()
        txtNVGioiTinh.DataBindings.Clear()
        txtDiaChi.DataBindings.Clear()
        txtDienThoai.DataBindings.Clear()
        'cmbLoaiNV = Nothing
    End Sub

    Private Sub LockTextboxNV(ByVal f As Boolean)
        cmbUserName.Enabled = f
        txtMatKhau.Enabled = f
        txtHoTen.Enabled = f
        dateNgaySinh.Enabled = f
        txtNVGioiTinh.Enabled = f
        txtDiaChi.Enabled = f
        txtDienThoai.Enabled = f
        cmbLoaiNV.Enabled = f
        If f = False Then
            cmbUserName.BackColor = Color.LemonChiffon
            txtMatKhau.BackColor = Color.LemonChiffon
            txtHoTen.BackColor = Color.LemonChiffon
            dateNgaySinh.BackColor = Color.LemonChiffon
            txtNVGioiTinh.BackColor = Color.LemonChiffon
            txtDiaChi.BackColor = Color.LemonChiffon
            txtDienThoai.BackColor = Color.LemonChiffon
            cmbLoaiNV.BackColor = Color.LemonChiffon
        Else
            cmbUserName.BackColor = Color.White
            txtMatKhau.BackColor = Color.White
            txtHoTen.BackColor = Color.White
            dateNgaySinh.BackColor = Color.White
            txtNVGioiTinh.BackColor = Color.White
            txtDiaChi.BackColor = Color.White
            txtDienThoai.BackColor = Color.White
            cmbLoaiNV.BackColor = Color.White
        End If
    End Sub

    Private Sub ClearTextboxNV()
        cmbUserName.Text = ""
        txtMatKhau.Text = ""
        txtHoTen.Text = ""
        dateNgaySinh.Text = ""
        txtNVGioiTinh.Text = ""
        txtDiaChi.Text = ""
        txtDienThoai.Text = ""
        'cmbLoaiNV = Nothing
    End Sub

    Private Sub LockButtonNV(ByVal dt As Boolean)
        btnNVThem.Enabled = dt
        btnNVSua.Enabled = dt
        btnNVXoa.Enabled = dt
        btnNVLuu.Enabled = Not dt
        btnNVHuy.Enabled = Not dt
    End Sub

    Private Sub btnNVSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNVSua.Click
        SuaNV()
    End Sub
    Private Sub SuaNV()
        flagIsUpNV = False
        LockTextboxNV(True)
        LockButtonNV(False)
        cmbUserName.Enabled = False
        cmbUserName.BackColor = Color.LemonChiffon
        connect = New ConnectDB()
        Dim i As Integer
        Dim dt As New DataTable
        dt = connect.GetData("Select IDLoaiNV From LOAINHANVIEN")
        For i = 0 To dt.Rows.Count() - 1
            dt.Rows(i)("IDLoaiNV").ToString()
        Next
        connect.Close()
        cmbLoaiNV.DataSource = dt
        cmbLoaiNV.DisplayMember = "IDLoaiNV"
        cmbLoaiNV.SelectedIndex = 0

        gvNhanVien.Enabled = False
        txtMatKhau.Focus()
    End Sub
    Private Sub btnNVHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNVHuy.Click
        ClearBindingNV()
        LoadDataNV()
        LockTextboxNV(False)
        LockButtonNV(True)
        gvNhanVien.Enabled = True
    End Sub

    Private Sub btnNVThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNVThem.Click
        ThemNV()
    End Sub

    Private Sub ThemNV()
        flagIsUpNV = True
        connect = New ConnectDB()
        Dim i As Integer
        Dim dt As New DataTable
        dt = connect.GetData("Select IDLoaiNV From LOAINHANVIEN")
        For i = 0 To dt.Rows.Count() - 1
            dt.Rows(i)("IDLoaiNV").ToString()
        Next
        connect.Close()
        cmbLoaiNV.DataSource = dt
        cmbLoaiNV.DisplayMember = "IDLoaiNV"
        cmbLoaiNV.SelectedIndex = 0
        LockTextboxNV(True)
        LockButtonNV(False)
        ClearTextboxNV()
        Dim ngay As Date = DateTime.Now.ToShortDateString()

        gvNhanVien.Enabled = False
        cmbUserName.Focus()
    End Sub

    Private Sub btnNVLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNVLuu.Click
        LuuNV()
    End Sub
    Private Sub LuuNV()
        Dim idnv As String = cmbUserName.Text
        Dim passnv As String = txtMatKhau.Text
        Dim hotennv As String = txtHoTen.Text
        Dim ngaysinh As Date = dateNgaySinh.Value
        Dim gioitinhnv As String = txtNVGioiTinh.Text
        Dim diachinv As String = txtDiaChi.Text
        Dim dienthoainv As String = txtDienThoai.Text
        Dim loainv As String = cmbLoaiNV.Text
        '-------------------------------------'
        If TestInfoNV() Then
            _nv = New NhanVienDTO(idnv, passnv, hotennv, ngaysinh, gioitinhnv, diachinv, dienthoainv, loainv)
            nvBUS = New NhanVienBUS()
            nvBUS.IsUpNV(_nv, flagIsUpNV)
            ClearBindingNV()
            LoadDataNV()
            LockTextboxNV(False)
            LockButtonNV(True)
            gvNhanVien.Enabled = True
            MsgBox("Đã cập nhật dữ liệu thành công", MsgBoxStyle.Information, "Thông báo")
        End If
    End Sub
    Private Sub txtDienThoai_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDienThoai.KeyPress

        If Not (Char.IsDigit(e.KeyChar) OrElse Char.IsControl(e.KeyChar)) Then Beep() : e.Handled = True
        ' nếu phím nhấn không phải là số hoặc phím     'control (nhấn "Backspace" để xóa số) -> thì beep cảnh báo và hủy sự kiện (không có phím được nhấn)

    End Sub
    Private Function TestInfoNV() As Boolean
        TestInfoNV = True
        If Trim(cmbUserName.Text) = "" Then
            TestInfoNV = False
            MsgBox("Tên đăng nhập không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            cmbUserName.Focus()
            Exit Function
        ElseIf Trim(txtMatKhau.Text) = "" Then
            TestInfoNV = False
            MsgBox("Mật khẩu không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtMatKhau.Focus()
            Exit Function
        ElseIf Trim(txtHoTen.Text) = "" Then
            TestInfoNV = False
            MsgBox("Họ tên không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtHoTen.Focus()
            Exit Function
        ElseIf Trim(dateNgaySinh.Text) = "" Then
            TestInfoNV = False
            MsgBox("Phải chọn ngày sinh không được bỏ trống! ", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtDiaChi.Focus()
            Exit Function
        ElseIf Trim(txtNVGioiTinh.Text) = "" Then
            If Trim(txtNVGioiTinh.Text) = "Nam" Or Trim(txtNVGioiTinh.Text) = "Nữ" Then
                TestInfoNV = True
            Else
                TestInfoNV = False
                MsgBox("Giới tính không được bỏ trống! hoặc giới tính bạn điền không có", MsgBoxStyle.Exclamation, "Thông báo lỗi")
                txtDiaChi.Focus()
                Exit Function
            End If
        ElseIf Trim(txtDiaChi.Text) = "" Then
            TestInfoNV = False
            MsgBox("Địa chỉ không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtDiaChi.Focus()
            Exit Function
        ElseIf Trim(txtDienThoai.Text) = "" Or txtDienThoai.TextLength > 11 Or txtDienThoai.TextLength < 10 Then
            TestInfoNV = False
            MsgBox("Điện thoại không được bỏ trống! số điện thoại không đúng", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtDienThoai.Focus()
            Exit Function
        End If
    End Function

    Private Sub btnNVXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNVXoa.Click
        XoaNV()
    End Sub
    Private Sub XoaNV()
        Dim user As String = cmbUserName.Text
        Dim qs = MsgBox("Bạn chắc chắn muốn xóa tất cả thông tin về:" _
                        & vbCrLf & vbCrLf & " - Nhân viên : " & txtHoTen.Text _
                        & vbCrLf & vbCrLf & " - Tên đăng nhập: " & cmbUserName.Text & " ", _
                        vbInformation + vbYesNo, "Thông báo")
        If qs = vbYes Then
            _nv = New NhanVienDTO(user)
            nvBUS = New NhanVienBUS()
            nvBUS.DeleteNV(_nv)
            ClearBindingNV()
            LoadDataNV()
            LockTextboxNV(False)
            LockButtonNV(True)
            gvNhanVien.Enabled = True
            MsgBox("Dữ liệu đã xóa thành công", vbInformation, "Thông báo")
        Else
            MsgBox("Đã hủy thao tác xóa!", vbExclamation, "Thông báo")
        End If
    End Sub
    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
        connect = New ConnectDB()
        Dim str As String
        Dim dt As DataTable = New DataTable()
        If txtTimNV.Text = "" Then
            MsgBox("Phải điền thông tin tìm!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTimNV.Focus()
            Exit Sub
        End If
        If rdNV.Checked = True Then
            str = "Select * From NHANVIEN WHERE IDNV LIKE'%" & txtTimNV.Text & "%'"
        Else
            str = "Select * From NHANVIEN WHERE HoTen LIKE'%" & txtTimNV.Text & "%'"
        End If
        Try
            ClearTextboxTT()
            gvNhanVien.ClearSelection()
            dt = connect.GetData(str)
            connect.Close()
            gvNhanVien.DataSource = dt
        Catch ex As Exception
            MsgBox("Không có!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
        End Try
    End Sub
#End Region
#Region "Xử lý nhân vien 2"
    Private Sub ButtonItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem1.Click
        ThemNV()
    End Sub

    Private Sub ButtonItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem3.Click
        XoaNV()
    End Sub

    Private Sub ButtonItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem4.Click
        SuaNV()
    End Sub

    Private Sub ButtonItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem5.Click
        LuuNV()
    End Sub

    Private Sub ButtonItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem6.Click
        txtTimNV.Text = ""
        rdNV.Checked = True
        ClearBindingNV()
        ClearTextboxNV()
        LoadDataNV()
    End Sub
    Private Sub ButtonX5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX5.Click
        txtTimNV.Text = ""
        rdNV.Checked = True
        ClearBindingNV()
        ClearTextboxNV()
        LoadDataNV()
    End Sub
    Private Sub ButtonItem31_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem31.Click
        frmDocFileExcel.ShowDialog()
        If frmDocFileExcel.duongdan.ToString = "" Then
            Exit Sub
        End If
        Dim chuoiketnoi As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
 "Data Source=" & frmDocFileExcel.txtFileExcel.Text & ";Extended Properties=""Excel 8.0;HDR=YES;"""
        Dim sheetname As String = frmDocFileExcel.tensheet.ToString
        Dim dtset As DataSet = Read(chuoiketnoi, sheetname)
        ClearBindingNV()
        ClearTextboxNV()
        gvNhanVien.DataSource = dtset.Tables.ToString
    End Sub

    Function Read(ByVal path As String, ByVal SheetName As String) As DataSet
        Dim myconnection As System.Data.OleDb.OleDbConnection = Nothing
        Try
            Dim Mycommand As System.Data.OleDb.OleDbDataAdapter = Nothing
            'Dim strConnectionString As String = "provider=Microsoft.Jet.OLEDB.4.0; data source= '" + Application.StartupPath + "' " / " '" + path + "'; Extended Properties=Excel 8.0;"
            myconnection = New System.Data.OleDb.OleDbConnection(path)
            Mycommand = New System.Data.OleDb.OleDbDataAdapter("select * from [" & SheetName & "$]", myconnection)
            Dim DtSet As DataSet = New DataSet
            Mycommand.Fill(DtSet)
            myconnection.Close()
            Return DtSet
        Catch ex As Exception
            myconnection.Close()
            Return Nothing
        End Try
    End Function

    Private Sub ButtonItem32_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem32.Click
        'verfying the datagridview having data or not
        If ((gvNhanVien.Columns.Count = 0) Or (gvNhanVien.Rows.Count = 0)) Then
            Exit Sub
        End If

        'Creating dataset to export
        Dim dset As New DataSet
        'add table to dataset
        dset.Tables.Add()
        'add column to that table
        For i As Integer = 0 To gvNhanVien.ColumnCount - 1
            dset.Tables(0).Columns.Add(gvNhanVien.Columns(i).HeaderText)
        Next
        'add rows to the table
        Dim dr1 As DataRow
        For i As Integer = 0 To gvNhanVien.RowCount - 1
            dr1 = dset.Tables(0).NewRow
            For j As Integer = 0 To gvNhanVien.Columns.Count - 1
                dr1(j) = gvNhanVien.Rows(i).Cells(j).Value
            Next
            dset.Tables(0).Rows.Add(dr1)
        Next

        Dim excel As New Microsoft.Office.Interop.Excel.Application  '.ApplicationClass
        Dim wBook As Microsoft.Office.Interop.Excel.Workbook
        Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

        wBook = excel.Workbooks.Add()
        wSheet = wBook.ActiveSheet()

        Dim dt As System.Data.DataTable = dset.Tables(0)
        Dim dc As System.Data.DataColumn
        Dim dr As System.Data.DataRow
        Dim colIndex As Integer = 0
        Dim rowIndex As Integer = 0

        For Each dc In dt.Columns
            colIndex = colIndex + 1
            excel.Cells(1, colIndex) = dc.ColumnName
        Next

        For Each dr In dt.Rows
            rowIndex = rowIndex + 1
            colIndex = 0
            For Each dc In dt.Columns
                colIndex = colIndex + 1
                excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)

            Next
        Next

        wSheet.Columns.AutoFit()
        frmExportvaImportToExcel.ShowDialog()
        If frmExportvaImportToExcel.duongdan = "" Then
            Exit Sub
        End If
        Dim strFileName As String = frmExportvaImportToExcel.duongdan.ToString
        Dim blnFileOpen As Boolean = False
        Try
            Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
            fileTemp.Close()
        Catch ex As Exception
            blnFileOpen = False
        End Try

        If System.IO.File.Exists(strFileName) Then
            System.IO.File.Delete(strFileName)
        End If

        wBook.SaveAs(strFileName)
        excel.Workbooks.Open(strFileName)
        excel.Visible = True
    End Sub


#End Region

#Region "xử lý load dữ liệu thông tin người đăng tin"
    Private ndtBUS As NguoiDangTinBUS
    Private _ndt As NguoiDangTinDTO
    Private ndt As CurrencyManager
    Private flagIsUpNDT As Boolean

    Private Sub LoadDataNDT()
        gvNguoiDangTin.ClearSelection()
        ndtBUS = New NguoiDangTinBUS()
        Dim empList As New List(Of NguoiDangTinDTO)
        empList = ndtBUS.Getdata()
        ndt = Me.BindingContext(empList)

        txtDTMa.DataBindings.Add("Text", empList, "Mã_đăng_tin")
        txtDTHoTen.DataBindings.Add("Text", empList, "Họ_tên")
        txtDTEmail.DataBindings.Add("Text", empList, "Email")
        txtDTDienThoai.DataBindings.Add("Text", empList, "Điện_thoại")
        txtDTDiaChi.DataBindings.Add("Text", empList, "Địa_chỉ")

        gvNguoiDangTin.DataSource = empList
    End Sub

    Private Sub ClearBindingNDT()
        txtDTMa.DataBindings.Clear()
        txtDTHoTen.DataBindings.Clear()
        txtDTEmail.DataBindings.Clear()
        txtDTDienThoai.DataBindings.Clear()
        txtDTDiaChi.DataBindings.Clear()
    End Sub

    Private Sub LockTextboxNDT(ByVal f As Boolean)
        txtDTHoTen.Enabled = f
        txtDTEmail.Enabled = f
        txtDTDienThoai.Enabled = f
        txtDTDiaChi.Enabled = f
        If f = False Then
            txtDTHoTen.BackColor = Color.LemonChiffon
            txtDTEmail.BackColor = Color.LemonChiffon
            txtDTDienThoai.BackColor = Color.LemonChiffon
            txtDTDiaChi.BackColor = Color.LemonChiffon
        Else
            txtDTHoTen.BackColor = Color.White
            txtDTEmail.BackColor = Color.White
            txtDTDienThoai.BackColor = Color.White
            txtDTDiaChi.BackColor = Color.White
        End If
    End Sub

    Private Sub ClearTextboxNDT()
        txtDTMa.Text = ""
        txtDTHoTen.Text = ""
        txtDTEmail.Text = ""
        txtDTDienThoai.Text = ""
        txtDTDiaChi.Text = ""
    End Sub

    Private Sub LockButtonNDT(ByVal dt As Boolean)
        btnDTThem.Enabled = dt
        btnDTSua.Enabled = dt
        btnDTXoa.Enabled = dt
        btnDTLuu.Enabled = Not dt
        btnDTHuy.Enabled = Not dt
    End Sub

    Private Sub btnDTSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDTSua.Click
        SuaDT()
    End Sub
    Private Sub SuaDT()
        flagIsUpNDT = False
        LockTextboxNDT(True)
        LockButtonNDT(False)
        gvNguoiDangTin.Enabled = False
        txtDTHoTen.Focus()
    End Sub


    Private Sub btnDTHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDTHuy.Click
        ClearBindingNDT()
        LoadDataNDT()
        LockTextboxNDT(False)
        LockButtonNDT(True)
        gvNguoiDangTin.Enabled = True
    End Sub

    Private Sub btnDTThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDTThem.Click
        ThemDT()
    End Sub
    Private Sub ThemDT()
        flagIsUpNDT = True
        LockTextboxNDT(True)
        LockButtonNDT(False)
        ClearTextboxNDT()
        gvNguoiDangTin.Enabled = False
        txtDTHoTen.Focus()
    End Sub

    Private Sub btnDTLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDTLuu.Click
        LuuDT()
    End Sub
    Private Sub LuuDT()
        Dim madt As String = txtDTMa.Text
        Dim hoten As String = txtDTHoTen.Text
        Dim email As String = txtDTEmail.Text
        Dim dt As String = txtDTDienThoai.Text
        Dim diachi As String = txtDTDiaChi.Text
        '-------------------------------------'
        If TestInfoNDT() Then
            _ndt = New NguoiDangTinDTO(madt, hoten, email, dt, diachi)
            ndtBUS = New NguoiDangTinBUS()
            ndtBUS.IsUpNDT(_ndt, flagIsUpNDT)
            ClearBindingNDT()
            LoadDataNDT()
            LockTextboxNDT(False)
            LockButtonNDT(True)
            gvNguoiDangTin.Enabled = True
            MsgBox("Đã cập nhật dữ liệu thành công", MsgBoxStyle.Information, "Thông báo")
        End If
    End Sub
    Private Sub txtDTDienThoai_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDTDienThoai.KeyPress
        If Not (Char.IsDigit(e.KeyChar) OrElse Char.IsControl(e.KeyChar)) Then Beep() : e.Handled = True
        ' nếu phím nhấn không phải là số hoặc phím     'control (nhấn "Backspace" để xóa số) -> thì beep cảnh báo và hủy sự kiện (không có phím được nhấn)
    End Sub
    Private Function TestInfoNDT() As Boolean
        TestInfoNDT = True
        If Trim(txtDTHoTen.Text) = "" Then
            TestInfoNDT()
            MsgBox("Firstname không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtDTHoTen.Focus()
            Exit Function
        ElseIf Trim(txtDTEmail.Text) = "" Then
            TestInfoNDT = False
            MsgBox("Lastname không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtDTEmail.Focus()
            Exit Function
        ElseIf Trim(txtDTDienThoai.Text) = "" Then
            TestInfoNDT = False
            MsgBox("Phone không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtDTDienThoai.Focus()
            Exit Function
        ElseIf Trim(txtDTDiaChi.Text) = "" Then
            TestInfoNDT = False
            MsgBox("Title không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtDTDiaChi.Focus()
            Exit Function
        End If
    End Function

    Private Sub btnDTXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDTXoa.Click
        XoaDT()
    End Sub
    Private Sub XoaDT()
        Dim EmpID As String = txtDTMa.Text
        Dim qs = MsgBox("Bạn chắc chắn muốn xóa tất cả thông tin về:" _
                        & vbCrLf & vbCrLf & " - Nhân viên ID: " & txtDTMa.Text _
                        & vbCrLf & vbCrLf & " - Tên: " & txtDTHoTen.Text & " ", _
                        vbInformation + vbYesNo, "Thông báo")
        If qs = vbYes Then
            _ndt = New NguoiDangTinDTO(EmpID)
            ndtBUS = New NguoiDangTinBUS()
            ndtBUS.DeleteNDT(_ndt)
            ClearBindingNDT()
            LoadDataNDT()
            LockTextboxNDT(False)
            LockButtonNDT(True)
            gvNguoiDangTin.Enabled = True
            MsgBox("Dữ liệu đã xóa thành công", vbInformation, "Thông báo")
        Else
            MsgBox("Đã hủy thao tác xóa!", vbExclamation, "Thông báo")
        End If
    End Sub
    Private Sub ButtonX16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX16.Click
        connect = New ConnectDB()
        Dim str As String
        Dim dt As DataTable = New DataTable()
        If txtND.Text = "" Then
            MsgBox("Phải điền thông tin tìm!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtND.Focus()
            Exit Sub
        End If
        If rdND.Checked = True Then
            str = "Select * From NGUOIDANGTIN WHERE MaDangTin LIKE'%" & txtND.Text & "%'"
        Else
            str = "Select * From NGUOIDANGTIN WHERE HoTen LIKE'%" & txtND.Text & "%'"
        End If
        Try
            ClearTextboxTT()
            gvNguoiDangTin.ClearSelection()
            dt = connect.GetData(str)
            connect.Close()
            gvNguoiDangTin.DataSource = dt
        Catch ex As Exception
            MsgBox("Không có!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
        End Try
    End Sub
#End Region
#Region "người đăng tin 2"
    Private Sub ButtonItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem2.Click
        ThemDT()
    End Sub

    Private Sub ButtonItem30_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem30.Click
        XoaDT()
    End Sub

    Private Sub ButtonItem36_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem36.Click
        SuaDT()
    End Sub

    Private Sub ButtonItem37_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem37.Click
        LuuDT()
    End Sub

    Private Sub ButtonItem38_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem38.Click
        txtND.Text = ""
        rdND.Checked = True
        ClearBindingNDT()
        ClearTextboxNDT()
        LoadDataNDT()
    End Sub

    Private Sub ButtonX11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX11.Click
        txtND.Text = ""
        rdND.Checked = True
        ClearBindingNDT()
        ClearTextboxNDT()
        LoadDataNDT()
    End Sub
    Private Sub ButtonItem39_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem39.Click

    End Sub
    Private Sub ButtonItem40_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem40.Click
        'verfying the datagridview having data or not
        If ((gvNguoiDangTin.Columns.Count = 0) Or (gvNguoiDangTin.Rows.Count = 0)) Then
            Exit Sub
        End If

        'Creating dataset to export
        Dim dset As New DataSet
        'add table to dataset
        dset.Tables.Add()
        'add column to that table
        For i As Integer = 0 To gvNguoiDangTin.ColumnCount - 1
            dset.Tables(0).Columns.Add(gvNguoiDangTin.Columns(i).HeaderText)
        Next
        'add rows to the table
        Dim dr1 As DataRow
        For i As Integer = 0 To gvNguoiDangTin.RowCount - 1
            dr1 = dset.Tables(0).NewRow
            For j As Integer = 0 To gvNguoiDangTin.Columns.Count - 1
                dr1(j) = gvNguoiDangTin.Rows(i).Cells(j).Value
            Next
            dset.Tables(0).Rows.Add(dr1)
        Next

        Dim excel As New Microsoft.Office.Interop.Excel.Application  '.ApplicationClass
        Dim wBook As Microsoft.Office.Interop.Excel.Workbook
        Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

        wBook = excel.Workbooks.Add()
        wSheet = wBook.ActiveSheet()

        Dim dt As System.Data.DataTable = dset.Tables(0)
        Dim dc As System.Data.DataColumn
        Dim dr As System.Data.DataRow
        Dim colIndex As Integer = 0
        Dim rowIndex As Integer = 0

        For Each dc In dt.Columns
            colIndex = colIndex + 1
            excel.Cells(1, colIndex) = dc.ColumnName
        Next

        For Each dr In dt.Rows
            rowIndex = rowIndex + 1
            colIndex = 0
            For Each dc In dt.Columns
                colIndex = colIndex + 1
                excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)

            Next
        Next

        wSheet.Columns.AutoFit()
        frmExportvaImportToExcel.ShowDialog()
        If frmExportvaImportToExcel.duongdan = "" Then
            Exit Sub
        End If
        Dim strFileName As String = frmExportvaImportToExcel.duongdan.ToString
        Dim blnFileOpen As Boolean = False
        Try
            Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
            fileTemp.Close()
        Catch ex As Exception
            blnFileOpen = False
        End Try

        If System.IO.File.Exists(strFileName) Then
            System.IO.File.Delete(strFileName)
        End If

        wBook.SaveAs(strFileName)
        excel.Workbooks.Open(strFileName)
        excel.Visible = True
    End Sub
#End Region

#Region "xử lý load dữ liệu thông tin người thuê"
    Private tnBUS As NguoiThueNhaBUS
    Private _tn As NguoiThueNhaDTO
    Private tn As CurrencyManager
    Private flagIsUpTN As Boolean


    Private Sub LoadDataTN()
        gvNguoiThueNha.ClearSelection()
        tnBUS = New NguoiThueNhaBUS()
        Dim empList As New List(Of NguoiThueNhaDTO)
        empList = tnBUS.Getdata()
        tn = Me.BindingContext(empList)

        txtTNMa.DataBindings.Add("Text", empList, "Mã_người_thuê")
        txtTNHoTen.DataBindings.Add("Text", empList, "Họ_tên")
        txtTNEmail.DataBindings.Add("Text", empList, "Email")
        txtTNNgheNghiep.DataBindings.Add("Text", empList, "Nghê_nghiệp")
        txtTNCMND.DataBindings.Add("Text", empList, "Số_CMND")
        txtTNDienThoai.DataBindings.Add("Text", empList, "Điện_Thoại")
        With cmbTNMaKV
            .DataSource = empList
            .DisplayMember = "Mã_Khu_Vực"
            .ValueMember = "Mã_Khu_Vực"
            .SelectedValue = "Mã_Khu_Vực"
        End With
        With cmbTNMaLN
            .DataSource = empList
            .DisplayMember = "Mã_loại_nhà"
            .ValueMember = "Mã_loại_nhà"
            .SelectedValue = "Mã_loại_nhà"
        End With

        gvNguoiThueNha.DataSource = empList
    End Sub

    Private Sub ClearBindingTN()
        txtTNMa.DataBindings.Clear()
        txtTNHoTen.DataBindings.Clear()
        txtTNEmail.DataBindings.Clear()
        txtTNNgheNghiep.DataBindings.Clear()
        txtTNCMND.DataBindings.Clear()
        txtTNDienThoai.DataBindings.Clear()
    End Sub

    Private Sub LockTextboxTN(ByVal f As Boolean)
        txtTNHoTen.Enabled = f
        txtTNEmail.Enabled = f
        txtTNNgheNghiep.Enabled = f
        txtTNCMND.Enabled = f
        txtTNDienThoai.Enabled = f
        cmbTNMaKV.Enabled = f
        cmbTNMaLN.Enabled = f
        If f = False Then
            txtTNHoTen.BackColor = Color.LemonChiffon
            txtTNEmail.BackColor = Color.LemonChiffon
            txtTNNgheNghiep.BackColor = Color.LemonChiffon
            txtTNCMND.BackColor = Color.LemonChiffon
            txtTNDienThoai.BackColor = Color.LemonChiffon
            cmbTNMaKV.BackColor = Color.LemonChiffon
            cmbTNMaLN.BackColor = Color.LemonChiffon
        Else
            txtTNHoTen.BackColor = Color.White
            txtTNEmail.BackColor = Color.White
            txtTNNgheNghiep.BackColor = Color.White
            txtTNCMND.BackColor = Color.White
            txtTNDienThoai.BackColor = Color.White
            cmbTNMaKV.BackColor = Color.White
            cmbTNMaLN.BackColor = Color.White
        End If
    End Sub

    Private Sub ClearTextboxTN()
        txtTNMa.Text = ""
        txtTNHoTen.Text = ""
        txtTNEmail.Text = ""
        txtTNNgheNghiep.Text = ""
        txtTNCMND.Text = ""
        txtTNDienThoai.Text = ""
    End Sub

    Private Sub LockButtonTN(ByVal dt As Boolean)
        btnTNThem.Enabled = dt
        btnTNSua.Enabled = dt
        btnTNXoa.Enabled = dt
        btnTNLuu.Enabled = Not dt
        btnTNHuy.Enabled = Not dt
    End Sub

    Private Sub btnTNSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTNSua.Click
        SuaTN()
    End Sub
    Private Sub SuaTN()
        flagIsUpTN = False
        LockTextboxTN(True)
        LockButtonTN(False)
        connect = New ConnectDB()
        Dim i As Integer
        Dim kv, ln As New DataTable
        kv = connect.GetData("Select MaKhuVuc From KHUVUC")
        For i = 0 To kv.Rows.Count() - 1
            kv.Rows(i)("MaKhuVuc").ToString()
        Next
        ln = connect.GetData("Select MaLoai From LOAINHA")
        For i = 0 To ln.Rows.Count() - 1
            ln.Rows(i)("MaLoai").ToString()
        Next
        connect.Close()
        cmbTNMaKV.DataSource = kv
        cmbTNMaKV.DisplayMember = "MaKhuVuc"
        cmbTNMaKV.SelectedIndex = 0

        cmbTNMaLN.DataSource = ln
        cmbTNMaLN.DisplayMember = "MaLoai"
        cmbTNMaLN.SelectedIndex = 0

        gvNguoiThueNha.Enabled = False
        txtTNHoTen.Focus()
    End Sub



    Private Sub btnTNHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTNHuy.Click
        ClearBindingTN()
        LoadDataTN()
        LockTextboxTN(False)
        LockButtonTN(True)
        gvNguoiThueNha.Enabled = True
    End Sub


    Private Sub btnTNThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTNThem.Click
        ThemTN()
    End Sub
    Private Sub ThemTN()
        flagIsUpTN = True
        LockTextboxTN(True)
        LockButtonTN(False)
        ClearTextboxTN()
        connect = New ConnectDB()
        Dim i As Integer
        Dim kv, ln As New DataTable
        kv = connect.GetData("Select MaKhuVuc From KHUVUC")
        For i = 0 To kv.Rows.Count() - 1
            kv.Rows(i)("MaKhuVuc").ToString()
        Next
        ln = connect.GetData("Select MaLoai From LOAINHA")
        For i = 0 To ln.Rows.Count() - 1
            ln.Rows(i)("MaLoai").ToString()
        Next
        connect.Close()
        cmbTNMaKV.DataSource = kv
        cmbTNMaKV.DisplayMember = "MaKhuVuc"
        cmbTNMaKV.SelectedIndex = 0

        cmbTNMaLN.DataSource = ln
        cmbTNMaLN.DisplayMember = "MaLoai"
        cmbTNMaLN.SelectedIndex = 0

        gvNguoiThueNha.Enabled = False
        txtTNHoTen.Focus()
    End Sub

    Private Sub btnTNLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTNLuu.Click
        LuuTN()
    End Sub
    Private Sub LuuTN()
        Dim Ma As String = txtTNMa.Text
        Dim Hoten As String = txtTNHoTen.Text
        Dim Email As String = txtTNEmail.Text
        Dim NgheNghiep As String = txtTNNgheNghiep.Text
        Dim CMND As String = txtTNCMND.Text
        Dim Dienthoai As String = txtTNDienThoai.Text
        Dim Khuvuc As String = cmbTNMaKV.Text
        Dim Loainha As String = cmbTNMaLN.Text
        '-------------------------------------'
        If TestInfoTN() Then
            _tn = New NguoiThueNhaDTO(Ma, Hoten, Email, NgheNghiep, CMND, Dienthoai, Khuvuc, Loainha)
            tnBUS = New NguoiThueNhaBUS()
            tnBUS.IsUpNT(_tn, flagIsUpTN)
            ClearBindingTN()
            LoadDataTN()
            LockTextboxTN(False)
            LockButtonTN(True)
            gvNguoiThueNha.Enabled = True
            MsgBox("Đã cập nhật dữ liệu thành công", MsgBoxStyle.Information, "Thông báo")
        End If
    End Sub
    Private Sub txtTNDienThoai_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTNDienThoai.KeyPress

        If Not (Char.IsDigit(e.KeyChar) OrElse Char.IsControl(e.KeyChar)) Then Beep() : e.Handled = True
        ' nếu phím nhấn không phải là số hoặc phím     'control (nhấn "Backspace" để xóa số) -> thì beep cảnh báo và hủy sự kiện (không có phím được nhấn)

    End Sub
    Private Function TestInfoTN() As Boolean
        TestInfoTN = True
        If Trim(txtTNHoTen.Text) = "" Then
            TestInfoTN = False
            MsgBox("Họ tên không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTNHoTen.Focus()
            Exit Function
        ElseIf Trim(txtTNEmail.Text) = "" Then
            TestInfoTN = False
            MsgBox("Email không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTNEmail.Focus()
            Exit Function
        ElseIf Trim(txtTNNgheNghiep.Text) = "" Then
            TestInfoTN = False
            MsgBox("Nghề nghiệp không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTNNgheNghiep.Focus()
            Exit Function
        ElseIf Trim(txtTNCMND.Text) = "" Then
            TestInfoTN = False
            MsgBox("Số chứng minh nhân dân không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTNDienThoai.Focus()
            Exit Function
        ElseIf Trim(txtTNDienThoai.Text) = "" Then
            TestInfoTN = False
            MsgBox("Điện thoại không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTNDienThoai.Focus()
            Exit Function
        End If
    End Function

    Private Sub btnTNXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTNXoa.Click
        XoaTN()
    End Sub
    Private Sub XoaTN()
        Dim EmpID As String = txtTNMa.Text
        Dim qs = MsgBox("Bạn chắc chắn muốn xóa tất cả thông tin về:" _
                        & vbCrLf & vbCrLf & " - Người thuê: " & txtTNMa.Text _
                        & vbCrLf & vbCrLf & " - Tên: " & txtTNHoTen.Text & " ", _
                        vbInformation + vbYesNo, "Thông báo")
        If qs = vbYes Then
            _tn = New NguoiThueNhaDTO(EmpID)
            tnBUS = New NguoiThueNhaBUS()
            tnBUS.DeleteNT(_tn)
            ClearBindingTN()
            LoadDataTN()
            LockTextboxTN(False)
            LockButtonTN(True)
            gvNguoiThueNha.Enabled = True
            MsgBox("Dữ liệu đã xóa thành công", vbInformation, "Thông báo")
        Else
            MsgBox("Đã hủy thao tác xóa!", vbExclamation, "Thông báo")
        End If
    End Sub
    Private Sub ButtonX15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX15.Click
        connect = New ConnectDB()
        Dim str As String
        Dim dt As DataTable = New DataTable()
        If txtNT.Text = "" Then
            MsgBox("Phải điền thông tin tìm!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtNT.Focus()
            Exit Sub
        End If
        If rdNT.Checked = True Then
            str = "Select * From NGUOITHUE WHERE MaNguoiThue LIKE'%" & txtNT.Text & "%'"
        Else
            str = "Select * From NGUOITHUE WHERE HoTenNT LIKE'%" & txtNT.Text & "%'"
        End If
        Try
            ClearTextboxTT()
            gvNguoiThueNha.ClearSelection()
            dt = connect.GetData(str)
            connect.Close()
            gvNguoiThueNha.DataSource = dt
        Catch ex As Exception
            MsgBox("Không có!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
        End Try
    End Sub
#End Region
#Region "người thuê nhà 2"
    Private Sub ButtonItem19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem19.Click
        ThemTN()
    End Sub

    Private Sub ButtonItem20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem20.Click
        XoaTN()
    End Sub

    Private Sub ButtonItem21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem21.Click
        SuaTN()
    End Sub

    Private Sub ButtonItem22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem22.Click
        LuuTN()
    End Sub

    Private Sub ButtonItem23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem23.Click
        txtNT.Text = ""
        rdNT.Checked = True
        ClearBindingTN()
        ClearTextboxTN()
        LoadDataTN()
    End Sub
    Private Sub ButtonX10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX10.Click
        txtNT.Text = ""
        rdNT.Checked = True
        ClearBindingTN()
        ClearTextboxTN()
        LoadDataTN()
    End Sub
    Private Sub ButtonItem24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem24.Click

    End Sub
    Private Sub ButtonItem25_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem25.Click
        'verfying the datagridview having data or not
        If ((gvNguoiThueNha.Columns.Count = 0) Or (gvNguoiThueNha.Rows.Count = 0)) Then
            Exit Sub
        End If

        'Creating dataset to export
        Dim dset As New DataSet
        'add table to dataset
        dset.Tables.Add()
        'add column to that table
        For i As Integer = 0 To gvNguoiThueNha.ColumnCount - 1
            dset.Tables(0).Columns.Add(gvNguoiThueNha.Columns(i).HeaderText)
        Next
        'add rows to the table
        Dim dr1 As DataRow
        For i As Integer = 0 To gvNguoiThueNha.RowCount - 1
            dr1 = dset.Tables(0).NewRow
            For j As Integer = 0 To gvNguoiThueNha.Columns.Count - 1
                dr1(j) = gvNguoiThueNha.Rows(i).Cells(j).Value
            Next
            dset.Tables(0).Rows.Add(dr1)
        Next

        Dim excel As New Microsoft.Office.Interop.Excel.Application  '.ApplicationClass
        Dim wBook As Microsoft.Office.Interop.Excel.Workbook
        Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

        wBook = excel.Workbooks.Add()
        wSheet = wBook.ActiveSheet()

        Dim dt As System.Data.DataTable = dset.Tables(0)
        Dim dc As System.Data.DataColumn
        Dim dr As System.Data.DataRow
        Dim colIndex As Integer = 0
        Dim rowIndex As Integer = 0

        For Each dc In dt.Columns
            colIndex = colIndex + 1
            excel.Cells(1, colIndex) = dc.ColumnName
        Next

        For Each dr In dt.Rows
            rowIndex = rowIndex + 1
            colIndex = 0
            For Each dc In dt.Columns
                colIndex = colIndex + 1
                excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)

            Next
        Next

        wSheet.Columns.AutoFit()
        frmExportvaImportToExcel.ShowDialog()
        If frmExportvaImportToExcel.duongdan = "" Then
            Exit Sub
        End If
        Dim strFileName As String = frmExportvaImportToExcel.duongdan.ToString
        Dim blnFileOpen As Boolean = False
        Try
            Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
            fileTemp.Close()
        Catch ex As Exception
            blnFileOpen = False
        End Try

        If System.IO.File.Exists(strFileName) Then
            System.IO.File.Delete(strFileName)
        End If

        wBook.SaveAs(strFileName)
        excel.Workbooks.Open(strFileName)
        excel.Visible = True
    End Sub
#End Region

#Region "xử lý load thông tin nhà"
    Private ttnBUS As ThongTinNhaBUS
    Private _ttn As ThongTinNhaDTO
    Private ttn As CurrencyManager
    Private flagIsUpTT As Boolean

    Private Sub LoadDataTT()
        ttnBUS = New ThongTinNhaBUS()
        Dim ttnList As New List(Of ThongTinNhaDTO)
        ttnList = ttnBUS.Getdata()
        ttn = Me.BindingContext(ttnList)

        txtTTMa.DataBindings.Add("Text", ttnList, "Mã_TTN")
        With cmbTTDangTin
            .DataSource = ttnList
            .DisplayMember = "Mã_đăng_tin"
            .ValueMember = "Mã_đăng_tin"
            .SelectedValue = "Mã_đăng_tin"
        End With
        With cmbTTLoaiNha
            .DataSource = ttnList
            .DisplayMember = "Mã_loại_nhà"
            .ValueMember = "Mã_loại_nhà"
            .SelectedValue = "Mã_loại_nhà"
        End With
        With cmbTTKhuVuc
            .DataSource = ttnList
            .DisplayMember = "Mã_khu_vực"
            .ValueMember = "Mã_khu_vực"
            .SelectedValue = "Mã_khu_vực"
        End With
        txtTTDiaChi.DataBindings.Add("Text", ttnList, "Địa_chỉ")
        txtTTDienTich.DataBindings.Add("Text", ttnList, "Diện_tích")
        txtTTGia.DataBindings.Add("Text", ttnList, "Giá_tiền")
        txtTTMoTa.DataBindings.Add("Text", ttnList, "Mô_tả")
        txtTTSucChua.DataBindings.Add("Text", ttnList, "Sức_chứa")
        txtTTHuongNha.DataBindings.Add("Text", ttnList, "Hướng_nhà")
        dateTTNgayDang.DataBindings.Add("Text", ttnList, "Ngày_đăng")
        dateTTNgayHet.DataBindings.Add("Text", ttnList, "Ngày_hết_hạn")

        gvThongTinNha.DataSource = ttnList
    End Sub

    Private Sub ClearBindingTT()
        txtTTMa.DataBindings.Clear()
        'cmbTTDangTin.DataBindings.Clear()
        'cmbTTLoaiNha.DataBindings.Clear()
        'cmbTTKhuVuc.DataBindings.Clear()
        txtTTDiaChi.DataBindings.Clear()
        txtTTDienTich.DataBindings.Clear()
        txtTTGia.DataBindings.Clear()
        txtTTMoTa.DataBindings.Clear()
        txtTTSucChua.DataBindings.Clear()
        txtTTHuongNha.DataBindings.Clear()
        dateTTNgayDang.DataBindings.Clear()
        dateTTNgayHet.DataBindings.Clear()
    End Sub

    Private Sub LockTextboxTT(ByVal f As Boolean)

        cmbTTDangTin.Enabled = f
        cmbTTLoaiNha.Enabled = f
        cmbTTKhuVuc.Enabled = f
        txtTTDiaChi.Enabled = f
        txtTTDienTich.Enabled = f
        txtTTGia.Enabled = f
        txtTTMoTa.Enabled = f
        txtTTSucChua.Enabled = f
        txtTTHuongNha.Enabled = f
        dateTTNgayDang.Enabled = f
        dateTTNgayHet.Enabled = f
        If f = False Then
            cmbTTDangTin.BackColor = Color.LemonChiffon
            cmbTTLoaiNha.BackColor = Color.LemonChiffon
            cmbTTKhuVuc.BackColor = Color.LemonChiffon
            txtTTDiaChi.BackColor = Color.LemonChiffon
            txtTTDienTich.BackColor = Color.LemonChiffon
            txtTTGia.BackColor = Color.LemonChiffon
            txtTTMoTa.BackColor = Color.LemonChiffon
            txtTTSucChua.BackColor = Color.LemonChiffon
            txtTTHuongNha.BackColor = Color.LemonChiffon
            dateTTNgayDang.BackColor = Color.LemonChiffon
            dateTTNgayHet.BackColor = Color.LemonChiffon
        Else
            cmbTTDangTin.BackColor = Color.White
            cmbTTLoaiNha.BackColor = Color.White
            cmbTTKhuVuc.BackColor = Color.White
            txtTTDiaChi.BackColor = Color.White
            txtTTDienTich.BackColor = Color.White
            txtTTGia.BackColor = Color.White
            txtTTMoTa.BackColor = Color.White
            txtTTSucChua.BackColor = Color.White
            txtTTHuongNha.BackColor = Color.White
            dateTTNgayDang.BackColor = Color.White
            dateTTNgayHet.BackColor = Color.White
        End If
    End Sub

    Private Sub ClearTextboxTT()
        txtTTMa.Text = ""
        'cmbTTDangTin.Text = ""
        'txtDTEmail.Text = ""
        'txtDTDienThoai.Text = ""
        txtTTDiaChi.Text = ""
        txtTTDienTich.Text = ""
        txtTTGia.Text = ""
        txtTTMoTa.Text = ""
        txtTTSucChua.Text = ""
        txtTTHuongNha.Text = ""
        dateTTNgayDang.Text = ""
        dateTTNgayHet.Text = ""
    End Sub

    Private Sub LockButtonTT(ByVal dt As Boolean)
        btnTTThem.Enabled = dt
        btnTTSua.Enabled = dt
        btnTTXoa.Enabled = dt
        btnTTLuu.Enabled = Not dt
        btnTTHuy.Enabled = Not dt
    End Sub

    Private Sub btnTTSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTTSua.Click
        SuaTT()
    End Sub
    Private Sub SuaTT()
        flagIsUpTT = False
        LockTextboxTT(True)
        LockButtonTT(False)
        txtTTMa.Enabled = False
        txtTTMa.BackColor = Color.LemonChiffon
        connect = New ConnectDB()
        Dim i As Integer
        Dim dt As New DataTable
        dt = connect.GetData("Select MaDangTin From NGUOIDANGTIN")
        For i = 0 To dt.Rows.Count() - 1
            dt.Rows(i)("MaDangTin").ToString()
        Next
        Dim dt2 As New DataTable
        dt2 = connect.GetData("Select MaKhuVuc From KHUVUC")
        For i = 0 To dt2.Rows.Count() - 1
            dt2.Rows(i)("MaKhuVuc").ToString()
        Next
        Dim dt3 As New DataTable
        dt3 = connect.GetData("Select MaLoai From LOAINHA")
        For i = 0 To dt3.Rows.Count() - 1
            dt3.Rows(i)("MaLoai").ToString()
        Next
        connect.Close()
        cmbTTDangTin.DataSource = dt
        cmbTTDangTin.DisplayMember = "MaDangTin"
        cmbTTDangTin.SelectedIndex = 0
        cmbTTKhuVuc.DataSource = dt2
        cmbTTKhuVuc.DisplayMember = "MaKhuVuc"
        cmbTTKhuVuc.SelectedIndex = 0
        cmbTTLoaiNha.DataSource = dt3
        cmbTTLoaiNha.DisplayMember = "MaLoai"
        cmbTTLoaiNha.SelectedIndex = 0

        gvThongTinNha.Enabled = False
        txtMatKhau.Focus()
    End Sub

    Private Sub btnTTHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTTHuy.Click
        ClearBindingTT()
        LoadDataTT()
        LockTextboxTT(False)
        LockButtonTT(True)
        gvThongTinNha.Enabled = True
    End Sub

    Private Sub btnTTThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTTThem.Click
        ThemTT()
    End Sub
    Private Sub ThemTT()
        flagIsUpTT = True
        connect = New ConnectDB()
        Dim i As Integer
        Dim dt As New DataTable
        dt = connect.GetData("Select MaDangTin From NGUOIDANGTIN")
        For i = 0 To dt.Rows.Count() - 1
            dt.Rows(i)("MaDangTin").ToString()
        Next
        Dim dt2 As New DataTable
        dt2 = connect.GetData("Select MaKhuVuc From KHUVUC")
        For i = 0 To dt2.Rows.Count() - 1
            dt2.Rows(i)("MaKhuVuc").ToString()
        Next
        Dim dt3 As New DataTable
        dt3 = connect.GetData("Select MaLoai From LOAINHA")
        For i = 0 To dt3.Rows.Count() - 1
            dt3.Rows(i)("MaLoai").ToString()
        Next
        connect.Close()
        cmbTTDangTin.DataSource = dt
        cmbTTDangTin.DisplayMember = "MaDangTin"
        cmbTTDangTin.SelectedIndex = 0
        cmbTTKhuVuc.DataSource = dt2
        cmbTTKhuVuc.DisplayMember = "MaKhuVuc"
        cmbTTKhuVuc.SelectedIndex = 0
        cmbTTLoaiNha.DataSource = dt3
        cmbTTLoaiNha.DisplayMember = "MaLoai"
        cmbTTLoaiNha.SelectedIndex = 0

        LockTextboxTT(True)
        LockButtonTT(False)
        ClearTextboxTT()
        Dim ngay As Date = DateTime.Now.ToShortDateString()

        gvThongTinNha.Enabled = False
        txtDiaChi.Focus()
    End Sub

    Private Sub btnTTLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTTLuu.Click
        LuuTT()
    End Sub
    Private Sub LuuTT()
        Dim madt As String = cmbTTDangTin.Text
        Dim mall As String = cmbTTLoaiNha.Text
        Dim makv As String = cmbTTKhuVuc.Text
        Dim dc As String = txtTTDiaChi.Text
        Dim dt As String = txtTTDienTich.Text
        Dim gt As String = txtTTGia.Text
        Dim mt As String = txtTTMoTa.Text
        Dim sc As String = txtTTSucChua.Text
        Dim hn As String = txtTTHuongNha.Text
        Dim nd As Date = dateTTNgayDang.Value
        Dim nh As Date = dateTTNgayHet.Value
        '-------------------------------------'
        If TestInfoNV() Then
            _ttn = New ThongTinNhaDTO(madt, mall, makv, dc, dt, gt, gt, mt, sc, hn, nd, nh)
            ttnBUS = New ThongTinNhaBUS()
            ttnBUS.IsUpTT(_ttn, flagIsUpTT)
            ClearBindingTT()
            LoadDataTT()
            LockTextboxTT(False)
            LockButtonTT(True)
            gvThongTinNha.Enabled = True
            MsgBox("Đã cập nhật dữ liệu thành công", MsgBoxStyle.Information, "Thông báo")
        End If
    End Sub

    Private Function TestInfoTT() As Boolean
        TestInfoTT = True
        If Trim(txtDiaChi.Text) = "" Then
            TestInfoTT = False
            MsgBox("Địa chỉ không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtDiaChi.Focus()
            Exit Function
        ElseIf Trim(txtTTDienTich.Text) = "" Then
            TestInfoTT = False
            MsgBox("Diện tich không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTTDienTich.Focus()
            Exit Function
        ElseIf Trim(txtTTGia.Text) = "" Then
            TestInfoTT = False
            MsgBox("Giá tiền không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTTGia.Focus()
            Exit Function
        ElseIf Trim(txtTTMoTa.Text) = "" Then
            TestInfoTT = False
            MsgBox("Mô tả không được bỏ trống! ", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTTMoTa.Focus()
            Exit Function
        ElseIf Trim(txtTTSucChua.Text) = "" Then
            TestInfoTT = False
            MsgBox("Sức chứa không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTTSucChua.Focus()
            Exit Function
        ElseIf Trim(txtTTHuongNha.Text) = "" Then
            TestInfoTT = False
            MsgBox("Hướng nhà không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTTHuongNha.Focus()
            Exit Function
        End If
    End Function


    Private Sub btnTTXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTTXoa.Click
        XoaTT()
    End Sub
    Private Sub XoaTT()
        Dim MaTT As String = txtTTMa.Text
        Dim qs = MsgBox("Bạn chắc chắn muốn xóa tất cả thông tin về:" _
                        & vbCrLf & vbCrLf & " - Ngôi nhà có mã : " & txtTTMa.Text _
                        & vbCrLf & vbCrLf & " - Của mã đăng tin: " & cmbTTDangTin.Text & " ", _
                        vbInformation + vbYesNo, "Thông báo")
        If qs = vbYes Then
            _ttn = New ThongTinNhaDTO(MaTT)
            ttnBUS = New ThongTinNhaBUS()
            ttnBUS.DeleteTT(_ttn)
            ClearBindingTT()
            LoadDataTT()
            LockTextboxTT(False)
            LockButtonTT(True)
            gvThongTinNha.Enabled = True
            MsgBox("Dữ liệu đã xóa thành công", vbInformation, "Thông báo")
        Else
            MsgBox("Đã hủy thao tác xóa!", vbExclamation, "Thông báo")
        End If
    End Sub
    Private Sub ButtonX9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX9.Click
        connect = New ConnectDB()
        Dim str As String
        Dim dt As DataTable = New DataTable()
        If txtTimDiaChi.Text = "" Then
            MsgBox("Phải điền thông tin tìm!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTimDiaChi.Focus()
            Exit Sub
        End If
        If rdDiachi.Checked = True Then
            str = "Select * From THONGTINNHA WHERE DiaChiNha LIKE'%" & txtTimDiaChi.Text & "%'"
        Else
            str = "Select * From THONGTINNHA WHERE MaThongTinNha LIKE'%" & txtTimDiaChi.Text & "%'"
        End If
        Try
            ClearTextboxTT()
            gvThongTinNha.ClearSelection()
            dt = connect.GetData(str)
            connect.Close()
            gvThongTinNha.DataSource = dt
        Catch ex As Exception
            MsgBox("Không có!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
        End Try
    End Sub
#End Region
#Region "thông tin nhà 2"
    Private Sub ButtonItem15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem15.Click
        ThemTT()
    End Sub

    Private Sub ButtonItem16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem16.Click
        XoaTT()
    End Sub

    Private Sub ButtonItem17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem17.Click
        SuaTT()
    End Sub

    Private Sub ButtonItem18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem18.Click
        LuuTT()
    End Sub

    Private Sub ButtonItem26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem26.Click
        txtTimDiaChi.Text = ""
        rdMaNha.Checked = True
        ClearBindingTT()
        ClearTextboxTT()
        LoadDataTT()
    End Sub
    Private Sub ButtonX6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX6.Click
        txtTimDiaChi.Text = ""
        rdMaNha.Checked = True
        ClearBindingTT()
        ClearTextboxTT()
        LoadDataTT()
    End Sub
    Private Sub ButtonItem27_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem27.Click

    End Sub
    Private Sub ButtonItem28_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem28.Click
        'verfying the datagridview having data or not
        If ((gvThongTinNha.Columns.Count = 0) Or (gvThongTinNha.Rows.Count = 0)) Then
            Exit Sub
        End If

        'Creating dataset to export
        Dim dset As New DataSet
        'add table to dataset
        dset.Tables.Add()
        'add column to that table
        For i As Integer = 0 To gvThongTinNha.ColumnCount - 1
            dset.Tables(0).Columns.Add(gvThongTinNha.Columns(i).HeaderText)
        Next
        'add rows to the table
        Dim dr1 As DataRow
        For i As Integer = 0 To gvThongTinNha.RowCount - 1
            dr1 = dset.Tables(0).NewRow
            For j As Integer = 0 To gvThongTinNha.Columns.Count - 1
                dr1(j) = gvThongTinNha.Rows(i).Cells(j).Value
            Next
            dset.Tables(0).Rows.Add(dr1)
        Next

        Dim excel As New Microsoft.Office.Interop.Excel.Application  '.ApplicationClass
        Dim wBook As Microsoft.Office.Interop.Excel.Workbook
        Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

        wBook = excel.Workbooks.Add()
        wSheet = wBook.ActiveSheet()

        Dim dt As System.Data.DataTable = dset.Tables(0)
        Dim dc As System.Data.DataColumn
        Dim dr As System.Data.DataRow
        Dim colIndex As Integer = 0
        Dim rowIndex As Integer = 0

        For Each dc In dt.Columns
            colIndex = colIndex + 1
            excel.Cells(1, colIndex) = dc.ColumnName
        Next

        For Each dr In dt.Rows
            rowIndex = rowIndex + 1
            colIndex = 0
            For Each dc In dt.Columns
                colIndex = colIndex + 1
                excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)

            Next
        Next

        wSheet.Columns.AutoFit()
        frmExportvaImportToExcel.ShowDialog()
        If frmExportvaImportToExcel.duongdan = "" Then
            Exit Sub
        End If
        Dim strFileName As String = frmExportvaImportToExcel.duongdan.ToString
        Dim blnFileOpen As Boolean = False
        Try
            Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
            fileTemp.Close()
        Catch ex As Exception
            blnFileOpen = False
        End Try

        If System.IO.File.Exists(strFileName) Then
            System.IO.File.Delete(strFileName)
        End If

        wBook.SaveAs(strFileName)
        excel.Workbooks.Open(strFileName)
        excel.Visible = True
    End Sub
#End Region

    '-------------------------------------------- xử lý các hàm load dữ liệu ------------------------------
 
    Public Sub Export_to_Excel(ByVal _DataSet As DataTable, ByVal _List_Col As String, ByVal Path As String)
        '- _Dataset: lưu dữu liệu cần Export
        '- _List_Col: Danh sách tên cột hiển thị. Tên cột cách nhau dấu ','
        '- Path: Đường dẫn lưu file excel

        Dim col, row As Integer
        Dim arrListcol() As String
        arrListcol = _List_Col.Split(",")

        If _DataSet.Rows.Count <= 0 Then
            Exit Sub
        End If

        Dim Excel As Object = CreateObject("Excel.Application")
        If Excel Is Nothing Then
            MessageBox.Show("Để xuất ra Excel, máy bạn phải cài đặt bộ phần mềm Microsoft Office.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        'Export to Excel process
        Try
            With Excel
                .SheetsInNewWorkbook = 1
                .Workbooks.Add()
                .Worksheets(1).Select()

                Dim i As Integer = 2
                .cells(1, 3).value = "Date:" + Format(Now, "MMM-dd-yyyy")
                ' .cells(1, 4).value = Format(Now, "MMM-dd-yyyy")
                .cells(2, i).value = "No"
                .cells(2, i).EntireRow.Font.Bold = True
                For col = 0 To arrListcol.Length - 1
                    i += 1
                    .cells(2, i).value = arrListcol(col).ToUpper()
                    .cells(2, i).EntireRow.Font.Name = "Times New Roman"
                    .cells(2, i).EntireRow.Font.Size = 10
                    .cells(2, i).EntireRow.Font.Bold = True
                Next
                i = 3
                Dim k As Integer = 3

                For col = 0 To _DataSet.Columns.Count - 1
                    i = 3

                    For row = 0 To _DataSet.Rows.Count - 1
                        .Cells(i, 2).Value = i - 2
                        .Cells(i, k).Value = _DataSet.Rows(row).ItemArray(col)
                        i += 1
                    Next
                    k += 1
                Next
                .Columns.AutoFit()
                .ActiveCell.Worksheet.SaveAs(Path)
            End With
            System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
            Excel = Nothing
            MessageBox.Show("Xuất dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Dim proc As System.Diagnostics.Process
            For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
                proc.Kill()
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Sub

    
    

    
    
    
    
   
   
    
    
    
End Class